package com.atguigu.webapp.service.impl;

import com.atguigu.model.pojo.ApartmentInfo;
import com.atguigu.model.pojo.FacilityInfo;
import com.atguigu.model.pojo.LabelInfo;
import com.atguigu.model.enums.ItemType;
import com.atguigu.webapp.mapper.*;
import com.atguigu.webapp.service.ApartmentInfoService;
import com.atguigu.webapp.vo.apartment.ApartmentDetailVo;
import com.atguigu.webapp.vo.apartment.ApartmentItemVo;
import com.atguigu.webapp.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


/**
 * @Author 梅超凡
 * @Date 2024/11/12~21:41
 * @Description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
*/
@Service
@Slf4j
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {


    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Override
    public ApartmentItemVo selectApartmentItemVoById(Long apartmentId) {

        // 查询公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(apartmentId);

        // 查询公寓的标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(apartmentId);

        // 查询公寓的图表信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, apartmentId);

        // 查询公寓中未被删除的房间的最低租金
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(apartmentId);

        //创建公寓信息vo对象
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        //属性对拷
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);

        log.info("查询结果：{}",apartmentItemVo);
        return apartmentItemVo;
    }


    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);
        if (apartmentInfo == null) {
            return null;
        }
        //2.查询GraphInfo
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        //3.查询LabelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        //4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        //5.查询公寓最低房租
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

        ApartmentDetailVo appApartmentDetailVo = new ApartmentDetailVo();

        BeanUtils.copyProperties(apartmentInfo, appApartmentDetailVo);
        appApartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted() == 1);
        appApartmentDetailVo.setGraphVoList(graphVoList);
        appApartmentDetailVo.setLabelInfoList(labelInfoList);
        appApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        appApartmentDetailVo.setMinRent(minRent);


        log.info("查询结果：{}",appApartmentDetailVo);
        return appApartmentDetailVo;
    }
}




